package com.seo.controller.user.role;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.seo.pojo.InputBody;
import com.seo.pojo.ReturnBody;
import com.seo.resolver.DefiniteException;
import com.seo.service.user.role.sdk.UserRoleService;
import com.seo.utils.RightsHelper;

import lombok.extern.slf4j.Slf4j;

/**
 * roleController <br/>
 * 
 * @author shibeilei
 * @version 2017.11.30 12:20
 */
@Slf4j
@Controller
@RequestMapping(value = "/user/role")
public class UserRoleController {

	@Autowired
	private UserRoleService userRoleService;

	@RequestMapping
	public String list(HttpServletRequest request, HttpSession session) {
		log.info("进入角色列表...");

		List<Map<String, Object>> roleList = userRoleService.getAllRoleList();

		request.setAttribute("roleList", roleList);
		return "user/role/list";

	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request) {
		log.info("进入新增角色页");
		request.setAttribute("action", "addRole");
		return "user/role/edit";
	}

	@RequestMapping("/addRole")
	public String addRole(HttpServletRequest request) {
		log.info("保存角色");
		InputBody body = InputBody.init();
		ReturnBody rb = ReturnBody.init();
		String roleName = body.get("roleName", String.class);
		if (StringUtils.isEmpty(roleName)) {
			request.setAttribute("result", rb.error("角色名称不能为空").getResult());
			return "save_result";
		}
		String sort = body.get("sort", String.class);
		if (sort == null) {
			request.setAttribute("result", rb.error("序号不能为空").getResult());
			return "save_result";
		}

		rb = userRoleService.saveRole(body.getParams());
		request.setAttribute("result", rb.getResult());
		return "save_result";
	}
	
	@RequestMapping("/edit")
	public String edit(Integer roleId, HttpServletRequest request) {
		log.info("进入编辑角色页");
		Map<String, Object> role= userRoleService.getRoleById(roleId);
		
		request.setAttribute("role", role);
		request.setAttribute("action", "editRole");
		
		return "user/role/edit";
	}
	
	@RequestMapping("/deleteRole")
	@ResponseBody
	public ReturnBody deleteMenu(Integer id) {
		log.info("删除菜单...");
		ReturnBody rb = ReturnBody.init();
		if (id == null || id <= 0) {
			return rb.error("error", "参数不能为空");
		}

		return userRoleService.deleteRole(id);
	}

	@RequestMapping("/editRole")
	public String editRole(HttpServletRequest request) {
		log.info("保存角色");
		InputBody body = InputBody.init();
		ReturnBody rb = ReturnBody.init();
		String id = body.get("id", String.class);
		if(!StringUtils.isNumeric(id)){
			request.setAttribute("result", rb.error("角色id格式错误").getResult());
			return "save_result";
		}
		
		String roleName = body.get("roleName", String.class);
		if (StringUtils.isEmpty(roleName)) {
			request.setAttribute("result", rb.error("角色名称不能为空").getResult());
			return "save_result";
		}
		String sort = body.get("sort", String.class);
		if (sort == null) {
			request.setAttribute("result", rb.error("序号不能为空").getResult());
			return "save_result";
		}

		rb = userRoleService.updateRole(body.getParams());
		request.setAttribute("result", rb.success().getResult());
		return "save_result";
	}

	@RequestMapping("/roleMenuTree")
	public String roleMenuTree(Integer roleId, String type, HttpServletRequest request, HttpSession session) {
		log.info("角色菜单界面...");

		if (roleId == null || roleId <= 0) {
			log.info("角色不存在");
			throw new DefiniteException("角色不存在");
		}
		if (StringUtils.isEmpty(type) || !("add".equals(type) || "del".equals(type) || "edit".equals(type)
				|| "cha".equals(type) || "rights".equals(type))) {
			log.info("参数错误");
			throw new DefiniteException("参数错误");
		}

		request.setAttribute("roleId", roleId);
		request.setAttribute("type", type);
		return "user/role/role_menu_tree";

	}

	@RequestMapping("/getRoleMenuList")
	@ResponseBody
	public ReturnBody getRoleMenuList(Integer roleId, String type, HttpServletRequest request, HttpSession session) {
		log.info("角色菜单列表...");
		return userRoleService.getRoleMenu(roleId, type);

	}

	@RequestMapping("/saveRoleMenu")
	@ResponseBody
	public ReturnBody saveRoleMenu(Integer roleId, String type, String menuIds[]) {
		ReturnBody rb = ReturnBody.init();
		if (roleId == null || roleId <= 0) {
			log.info("角色不存在");
			return rb.error("error", "角色不存在");
		}

		if (menuIds == null) {
			log.info("菜单不存在");
			return rb.error("error", "菜单不存在");
		}
		Map<String, Object> params = new HashMap<>();
		params.put("id", roleId);
		params.put("type", type);
		if (menuIds.length == 0) {
			params.put("value", "0");
		} else {
			BigInteger rights = RightsHelper.sumRights(menuIds);
			params.put("value", rights.toString());
		}
		int num = userRoleService.updateRoleRights(params);
		if (num == 0) {
			log.info("更新角色菜单失败");
			return rb.error();
		}

		return rb.success();

	}
}
